package com.shanjupay.merchant;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author xiams
 * @Date: 2021/5/17 17:11
 */

@SpringBootApplication
@EnableDiscoveryClient
public class MerchantApplicationBootstrap {

    public static void main(String[] args) {
        SpringApplication.run(MerchantApplicationBootstrap.class, args);
    }


    /**
     * 通过构造方法注入RestTemplate,具体使用okHttp的工厂方法创建出Bean(创建出的是一个默认实例对象);
     *
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        // 使用第三方的http类库进行restful的模拟的请求
        RestTemplate template = new RestTemplate(new OkHttp3ClientHttpRequestFactory());
        // 设置消息转换器
        List<HttpMessageConverter<?>> messageConverters = template.getMessageConverters();
        // 设置字符集编码
        messageConverters.set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return template;
    }

}
